/**
 * 
 */
package com.wilmar.process.dao.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.dao.api.IProcessLogDAO;
import com.wilmar.process.entity.ProcessLog;
import com.wilmar.process.entity.User;
import com.wilmar.process.entity.query.ProcessLogQueryObject;
import com.wilmar.process.enumerate.EnumProcessLogLevel;

/**
 * @author MaKuBEX E-mail:makubexnet@gmail.com
 * @version 1.0.0 at Jul 31, 2009 2:44:20 PM
 * 
 */
public class ProcessLogDAOImpl extends BaseDAOImpl<ProcessLog, Long> implements
		IProcessLogDAO {

	/**
	 * @see com.wilmar.process.dao.api.IProcessLogDAO#getListByQueryObject(com.wilmar.process.entity.query.ProcessLogQueryObject)
	 */
	public List<ProcessLog> getListByQueryObject(ProcessLogQueryObject prototype) {
		StringBuffer hql = new StringBuffer();
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_SELECT);
		hql.append("processLog");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_FROM);
		hql.append(ProcessLog.class.getName());
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
		hql.append("processLog");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_WHERE);
		hql.append("1=1");
		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);
		if (prototype.getInstanceStarter() != null
				&& prototype.getInstanceStarter().length != 0
				&& prototype.getInstanceStarter()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.instanceStarter");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : prototype.getInstanceStarter()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getMinLogLevel() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.logLevel");
			hql.append(ProcessSystemDeclareKeys.GREATER_EQUAL);
			hql.append(prototype.getMinLogLevel().getValue());
		}
		if (prototype.getProcessDefinitionKey() != null
				&& prototype.getProcessDefinitionKey().length != 0
				&& prototype.getProcessDefinitionKey()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String pdk : prototype.getProcessDefinitionKey()) {
				if (pdk != null && pdk.trim().length() != 0) {
					hql.append("processLog.processInstanceId");
					hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_LIKE);
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdk.trim());
					hql.append(ProcessSystemDeclareKeys.PERCENTAGE);
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_OR);
				}
			}
			hql.delete(hql.length()
					- ProcessSystemDeclareKeys.SQL_STATEMENT_OR.length(), hql
					.length());
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getLogLevel() != null
				&& prototype.getLogLevel().length != 0
				&& prototype.getLogLevel()[0] != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.logLevel");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (EnumProcessLogLevel l : prototype.getLogLevel()) {
				if (l != null) {
					hql.append(l.getValue());
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getMaxEnterTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.enterTime");
			hql.append(ProcessSystemDeclareKeys.LESS_EQUAL);
			hql.append("to_date(");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(prototype.getMaxEnterTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(")");
		}
		if (prototype.getMinEnterTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.enterTime");
			hql.append(ProcessSystemDeclareKeys.GREATER_EQUAL);
			hql.append("to_date(");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(prototype.getMinEnterTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(")");
		}
		if (prototype.getMaxLeaveTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.leaveTime");
			hql.append(ProcessSystemDeclareKeys.LESS_EQUAL);
			hql.append("to_date(");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(prototype.getMaxLeaveTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(")");
		}
		if (prototype.getMinLeaveTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.leaveTime");
			hql.append(ProcessSystemDeclareKeys.GREATER_EQUAL);
			hql.append("to_date(");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(prototype.getMinLeaveTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(")");
		}
		if (prototype.getProcessDefinitionName() != null
				&& prototype.getProcessDefinitionName().length != 0
				&& prototype.getProcessDefinitionName()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.processDefinitionName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : prototype.getProcessDefinitionName()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getProcessInstanceId() != null
				&& prototype.getProcessInstanceId().length != 0
				&& prototype.getProcessInstanceId()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.processInstanceId");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : prototype.getProcessInstanceId()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getOutSystem() != null
				&& prototype.getOutSystem().length != 0
				&& prototype.getOutSystem()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.outerSystem");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : prototype.getOutSystem()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getTaskInstanceId() != null
				&& prototype.getTaskInstanceId().length != 0
				&& prototype.getTaskInstanceId()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.executionId");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : prototype.getTaskInstanceId()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.isStartLog()) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.instanceStarter");
			hql.append(ProcessSystemDeclareKeys.SPACE);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IS);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_NOT);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_NULL);
		}
		if (prototype.getOperators() != null
				&& prototype.getOperators().length != 0
				&& prototype.getOperators()[0] != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("processLog.operator");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (User u : prototype.getOperators()) {
				if (u != null) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(u.getId().trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (prototype.getPageination() != null
				&& prototype.getPageination().getLimit() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("rownum");
			hql.append(ProcessSystemDeclareKeys.LESS_EQUAL);
			hql.append(prototype.getPageination().getLimit());
		}
		return super.getListByHql(hql.toString());
	}
}
